def character_frequency(in_file):
    
    counts = dict()

    for line in in_file:
        for c in line:
            c = str.lower(c)
            counts[c] = counts.get(c,0) + 1
    
    return sorted(list(counts.items()), key=lambda x : x[1], reverse=True)


def word_frequency(in_file):

    counts = dict()

    for line in in_file:
        for c in '.,;:?!"#$()':
            line = line.replace(c, ' ')

        for word in line.split():
            w = word.lower()
            counts[w] = counts.get(w,0) + 1

    return sorted(list(counts.items()), key=lambda x : x[1], reverse=True)


file_name = "text.txt"

in_file = open(file_name, "r", encoding='utf-8')
out_file = open(file_name + ".result_1.txt", "w")

cnt = 0

word_exclude = {'a', 'the', 'and', 'of', 'to', 'he', 'she', 'we', 'us', 'her', 'him', 'his', 'your', 'my', 'they', 'them', 'you', 'me', 'i'}

for item in word_frequency(in_file):

    if item[0] in word_exclude:
        continue

    s = "{:<20}{:>5}".format(item[0], item[1])
    if cnt < 10: 
        print(s)
        cnt = cnt + 1
    out_file.write(s + '\n')


in_file.close()
out_file.close()

in_file = open(file_name, "r", encoding='utf-8')
out_file = open(file_name + ".result_2.txt", "w")

# character_exclude = {' ', '.', '/', '"', '(', ')', '\t', '\n', '?', '!'}

character_exclude = " .!?()\t\n,\"'"

cnt = 0

for item in character_frequency(in_file):

    if item[0] in character_exclude:
        continue

    s = "{:<5}{:>10}".format(item[0], item[1])
    if cnt < 5: 
        print(s)
        cnt = cnt + 1
    out_file.write(s + '\n')
